import { BT_State } from "../../BTDefine";
import { Decorator } from "./Decorator";

/**
 * 重复装饰实例，只要子节点执行成功过一次，返回true
 */
export class Repeat extends Decorator {
    protected _repeatCount = 2;
    private _successNum = 0;

    public onEnter(): void {

    }

    public onExit(): void {

    }

    public onProcess(dt: number): void {
        if (this._repeatCount > 0) {
            let ret = this._child.tick(dt);
            if (!this._child.isRunning()) {
                this._child.reset();
                this._repeatCount--;

                if (ret === BT_State.SUCCESS) {
                    this._successNum++;
                }
            }
        } else {
            if (this._successNum > 0)
                this.setResult(BT_State.SUCCESS);
            else
                this.setResult(BT_State.FAILURE);
        }
    }

}